//
//  An_PopViewController.m
//  Animation_UIview1
//
//  Created by 刘文超 on 2019/12/24.
//  Copyright © 2019 刘文超. All rights reserved.
//

#import "An_PopViewController.h"
#import <pop/POP.h>
@interface An_PopViewController ()
@property(nonatomic,strong)UIView *ls_bgView1;
@property(nonatomic,strong)UIView *ls_bgView2;
@property(nonatomic,strong)UIView *ls_bgView3;
@property(nonatomic,strong)UILabel *ls_label;
@end

@implementation An_PopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGFloat view_width = ([UIScreen mainScreen].bounds.size.width-100);
    UIView *bgView1=[[UIView alloc]initWithFrame:CGRectMake(50, 100, view_width, 100)];
    [self.view addSubview:bgView1];
    bgView1.backgroundColor= [UIColor redColor];
    self.ls_bgView1 = bgView1;
    
    
    UIView *bgView2=[[UIView alloc]initWithFrame:CGRectMake(50, 200, view_width/2,100)];
    [self.view addSubview:bgView2];
    bgView2.backgroundColor = [UIColor blueColor];;
    self.ls_bgView2 = bgView2;
    
    
    UIView *bgView3=[[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(bgView2.frame), 200, view_width/2,100)];
    [self.view addSubview:bgView3];
    bgView3.userInteractionEnabled = YES;
    bgView3.backgroundColor = [UIColor greenColor];;
    self.ls_bgView3 = bgView3;
    
    
    UIPanGestureRecognizer *tap=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];
    [bgView3 addGestureRecognizer:tap];
    
    
    UILabel *leftLabel=[[UILabel alloc]initWithFrame:CGRectMake(50, 200, 100, 30)];
    [self.view addSubview:leftLabel];
    self.ls_label = leftLabel;
    leftLabel.font=[UIFont systemFontOfSize:14 ];
    leftLabel.text=@"";
    leftLabel.backgroundColor=[UIColor clearColor];
}
#pragma mark --衰减效果
-(void)tapClick:(UIPanGestureRecognizer *)tap
{
    if (tap.state == UIGestureRecognizerStateChanged) {
        
        CGPoint velocity = [tap locationInView:self.view];
       
        CGPoint translation = [tap translationInView:self.view];
        
    
        tap.view.center = CGPointMake(tap.view.center.x + translation.x,tap.view.center.y + translation.y);
        [tap setTranslation:CGPointZero inView:self.view];

        NSLog(@"----------%@",NSStringFromCGPoint(translation));
        // 1.初始化
        POPDecayAnimation *animtion = [POPDecayAnimation animationWithPropertyNamed:kPOPLayerPosition];
        // 2.设置初始值
        // 这个例子中不需要设置初始值。注意：POPDecayAnimation只有fromValue，没有toValue
        // POPDecayAnimation设置duration也是没有意义的，因为POPDecayAnimation的动画持续时间，是由velocity(速度)和deceleration(衰减系数)决定的。
        // 衰减系数（越小贼衰减的越快）很少用到，可以不设置
        animtion.deceleration = 0.998;
        // 设置动画速度
        animtion.velocity = [NSValue valueWithCGPoint:velocity];
        // 3.添加到view上
        [tap.view.layer pop_addAnimation:animtion forKey:@"positionAnimation"];
    }
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    switch (self.type) {
        case AnpopStyle_tanhuang:
             [self ls_TanhuangAnimation];
            break;
        case AnpopStyle_shuaitui:
            
            break;
        case AnpopStyle_jichu:
              [self ls_BaseAnimation];
            break;
        case AnpopStyle_zidingyi:
             [self ls_customAnimation];
            break;
            
        default:
            break;
    }
   
   
   
}
#pragma mark -- 基本动画
-(void)ls_BaseAnimation
{
    POPBasicAnimation *animation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewBackgroundColor];
    animation.duration = 3;
    animation.fromValue = [UIColor redColor];
    animation.toValue = [UIColor greenColor];
    // 动画类型 系统预设的类型有以下5种：
    //    kCAMediaTimingFunctionLinear            线性,即匀速
    //    kCAMediaTimingFunctionEaseIn            先慢后快
    //    kCAMediaTimingFunctionEaseOut           先快后慢
    //    kCAMediaTimingFunctionEaseInEaseOut     先慢后快再慢
    //    kCAMediaTimingFunctionDefault           实际效果是动画中间比较快
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    animation.beginTime = CACurrentMediaTime()+1;
    [self.ls_bgView3 pop_addAnimation:animation forKey:@"colorAnimation"];
}
#pragma mark -- 自定义动画
-(void)ls_customAnimation
{
    
    POPAnimatableProperty *prop = [POPAnimatableProperty propertyWithName:@"custom" initializer:^(POPMutableAnimatableProperty *prop) {
        
        // 告诉pop当前的属性值
        prop.readBlock = ^(id obj, CGFloat *values) {
            
        };
        // 修改变化后的属性值
        prop.writeBlock = ^(id obj, const CGFloat *values) {
            UILabel *label = (UILabel *)obj;
            label.text = [NSString stringWithFormat:@"%02d:%02d:%02d",(int)values[0]/60,(int)values[0]%60,(int)(values[0]*100)%100];
        };
        // 动画变化的快慢，值越大block调用的次数越少
        prop.threshold = 0.1;
        
    }];
    
    // 1.初始化
    POPBasicAnimation * anim = [POPBasicAnimation linearAnimation];
    
    // 自定义属性
    anim.property = prop;
    // 2.设置初始值
    anim.fromValue = @(3 * 60);
    anim.toValue = @(0);
    // 动画的时长
    anim.duration = 3 * 60;
    anim.beginTime = CACurrentMediaTime() + 1.f;
    [self.ls_label pop_addAnimation:anim forKey:@"countAnimation"];
}
#pragma mark --弹簧效果
-(void)ls_TanhuangAnimation
{
    
    POPSpringAnimation *animation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionY];
    animation.fromValue = @(10);
    animation.toValue = @(500);
    animation.springSpeed = 2;// 速度 可以设置的范围是0-20，默认为12.值越大速度越快，结束的越快
    animation.springBounciness = 10;// 振幅 可以设置的范围是0-20，默认为4。值越大振动的幅度越大
    // 拉力 拉力越大，动画的速度越快，结束的越快。 接下来的三个值一般不用设置，可以分别放开注释查看效果
    //  animation.dynamicsTension = 250;
    // 摩擦力 摩擦力越大，动画的速度越慢，振动的幅度越小。
    //  animation.dynamicsFriction = 100.0;
    // 质量 质量越大，动画的速度越慢，振动的幅度越大，结束的越慢
    animation.dynamicsMass = 10;
    animation.beginTime = CACurrentMediaTime() + 1.f;
    [self.ls_bgView1.layer pop_addAnimation:animation forKey:@"Position_xy"];
    
}

@end
